package com.sinodata.bsm.center.service.fault;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.cache.TrapRuleCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.impl.TrapRuleDaoImpl;
import com.sinodata.bsm.common.vo.FaultTrapRule;
import com.sinodata.bsm.common.vo.Res;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-18 下午11:06:40          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class TrapRuleService {

    private static final Logger logger = Logger.getLogger(TrapRuleService.class);

    @Autowired
    private BaseDao baseDao;

    @Autowired
    private TrapRuleCache trapRuleCache;

    @Autowired
    private ResCache resCache;

    @Autowired
    private TrapRuleDaoImpl trapRuleDaoImpl;

    public void saveOrUpdate(FaultTrapRule rule) {
        baseDao.saveOrUpdate(rule);
    }

    /**
     * 
     * @param id
     */
    public void delete(Long id) {
        trapRuleDaoImpl.delete(id);
    }

    /**
     * 根据trap的ip来查找对应的资源
     * @param ip
     * @return
     */
    public Long getResByTrapIp(String ip) {
        if (ip == null || ip.length() == 0) {
            return null;
        }
        //        List<Res> resList = resCache.getResByIp(ip.trim());

        List<FaultTrapRule> ruleList = trapRuleCache.getAll();
        for (FaultTrapRule rule : ruleList) {
            Long id = rule.getResId();
            if (id != null && id > 0) {
                if (ip.equals(resCache.get(id).getIp())) {
                    return id;
                }
            } else {
                continue;
            }
        }

        for (FaultTrapRule rule : ruleList) {
            Long typeId = rule.getResType();
            Res res = resCache.getResByIpType(ip, typeId);
            if (res != null) {
                return res.getId();
            } else {
                continue;
            }
        }

        return null;
    }

    /**
     * 查看一个ip的资源有没有配置trap事件规则
     * @param ip
     * @return
     */
    public List<FaultTrapRule> getTrapRuleByIp(String ip) {
        List<FaultTrapRule> list = new ArrayList<FaultTrapRule>();
        if (ip == null || ip.length() == 0) {
            return null;
        }
        //        List<Res> resList = resCache.getResByIp(ip.trim());

        List<FaultTrapRule> ruleList = trapRuleCache.getAll();
        for (FaultTrapRule rule : ruleList) {
            Long id = rule.getResId();
            if (id != null && id > 0) {
                Res res = resCache.get(id);
                if (res != null && ip.equals(res.getIp())) {
                    rule.setResId(id);
                    list.add(rule);
                }
            } else {
                continue;
            }
        }

        for (FaultTrapRule rule : ruleList) {
            Long typeId = rule.getResType();
            Res res = resCache.getResByIpType(ip, typeId);
            if (res != null) {
                rule.setResId(res.getId());
                list.add(rule);
            } else {
                continue;
            }
        }

        return list;
    }
}
